﻿Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Drawing
Imports System.IO
Imports System.Windows.Forms
Imports System.Workflow.ComponentModel

Namespace System.Workflow.ComponentModel.Design
    <ActivityDesignerTheme(GetType(ActivityPreviewDesignerTheme)), SRCategory("ActivityPreviewDesigners", "System.Workflow.ComponentModel.Design.DesignerResources")> _
    Public Class ActivityPreviewDesigner
        Inherits SequentialActivityDesigner
        ' Methods
        Public Sub New()
            Me.previewStrip = New PreviewItemStrip(Me)
            AddHandler Me.previewStrip.SelectionChanged, New SelectionChangeEventHandler([Of] SelectionChangeEventArgs)(AddressOf Me.OnPreviewChanged)
            Me.previewStrip.HelpText = DR.GetString("DropActivitiesHere", New Object(0  - 1) {})
        End Sub

        Protected Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                Dim service As ISelectionService = [TryCast](MyBase.GetService(GetType(ISelectionService)),ISelectionService)
                If (Not service Is Nothing) Then
                    RemoveHandler service.SelectionChanged, New EventHandler(AddressOf Me.OnSelectionChanged)
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub

        Public Overrides Sub EnsureVisibleContainedDesigner(ByVal containedDesigner As ActivityDesigner)
            If (containedDesigner Is Nothing) Then
                Throw New ArgumentNullException("containedDesigner")
            End If
            MyBase.EnsureVisibleContainedDesigner(containedDesigner)
            If (MyBase.ActiveDesigner Is Me) Then
                Dim info As ItemInfo
                For Each info In Me.previewStrip.Items
                    If (info.UserData.Item(DesignerUserDataKeys.Activity) Is containedDesigner.Activity) Then
                        Me.previewStrip.ActiveItem = info
                        Exit For
                    End If
                Next
            End If
        End Sub

        Protected Friend Overrides Function GetConnectors() As Rectangle()
            If ((Not Me.Expanded OrElse (Me.ContainedDesigners.Count > 0)) OrElse (Me.ShowPreview OrElse (Not MyBase.ActiveDesigner Is Me))) Then
                Return New Rectangle(0  - 1) {}
            End If
            Dim ambientTheme As AmbientTheme = WorkflowTheme.CurrentTheme.AmbientTheme
            Dim num As Integer = (ambientTheme.Margin.Height + Me.previewStrip.Size.Height)
            num = (num + ambientTheme.Margin.Height)
            Dim connectors As Rectangle() = MyBase.GetConnectors
            If (connectors.Length > 0) Then
                connectors(0).Y = (connectors(0).Y + num)
                connectors(0).Height = (connectors(0).Height - num)
            End If
            Return connectors
        End Function

        Protected Overrides Function GetDropTargets(ByVal dropPoint As Point) As Rectangle()
            If ((MyBase.ActiveDesigner Is Me) AndAlso Me.previewStrip.Bounds.Contains(dropPoint)) Then
                Return Me.previewStrip.DropTargets
            End If
            Return MyBase.GetDropTargets(dropPoint)
        End Function

        Public Overrides Function GetNextSelectableObject(ByVal obj As Object, ByVal direction As DesignerNavigationDirection) As Object
            If (Not MyBase.ActiveDesigner Is Me) Then
                Return MyBase.GetNextSelectableObject(obj, direction)
            End If
            If ((direction <> DesignerNavigationDirection.Left) AndAlso (direction <> DesignerNavigationDirection.Right)) Then
                Return Nothing
            End If
            Dim obj2 As Object = Nothing
            Dim num As Integer = Me.StripItemIndexFromActivity([TryCast](obj,Activity))
            If ((direction = DesignerNavigationDirection.Left) AndAlso (num >= 0)) Then
                Return Me.previewStrip.Items.Item(If((num > 0), (num - 1), (Me.previewStrip.Items.Count - 1))).UserData.Item(DesignerUserDataKeys.Activity)
            End If
            If ((direction = DesignerNavigationDirection.Right) AndAlso (num <= (Me.previewStrip.Items.Count - 1))) Then
                obj2 = Me.previewStrip.Items.Item(If((num < (Me.previewStrip.Items.Count - 1)), (num + 1), 0)).UserData.Item(DesignerUserDataKeys.Activity)
            End If
            Return obj2
        End Function

        Public Overrides Function HitTest(ByVal point As Point) As HitTestInfo
            Dim nowhere As HitTestInfo = HitTestInfo.Nowhere
            If (Me.Expanded AndAlso (MyBase.ActiveDesigner Is Me)) Then
                If ((Me.ContainedDesigners.Count = 0) AndAlso Me.HelpTextRectangle.Contains(point)) Then
                    Return New ConnectorHitTestInfo(Me, HitTestLocations.Designer, 0)
                End If
                If Me.previewStrip.Bounds.Contains(point) Then
                    Dim info2 As ItemInfo = Me.previewStrip.HitTest(point)
                    Dim designer As ActivityDesigner = If((Not info2 Is Nothing), ActivityDesigner.GetDesigner([TryCast](info2.UserData.Item(DesignerUserDataKeys.Activity),Activity)), Nothing)
                    If (Not designer Is Nothing) Then
                        Return New HitTestInfo(designer, HitTestLocations.Designer)
                    End If
                    Return New HitTestInfo(Me, (HitTestLocations.ActionArea Or HitTestLocations.Designer))
                End If
                If ((Me.ShowPreview AndAlso Me.previewWindow.Bounds.Contains(point)) AndAlso ((Me.previewWindow.PreviewMode OrElse (Me.PreviewedDesigner Is Nothing)) OrElse Not Me.PreviewedDesigner.Bounds.Contains(point))) Then
                    Return New HitTestInfo(Me, (HitTestLocations.ActionArea Or HitTestLocations.Designer))
                End If
                nowhere = MyBase.HitTest(point)
                If ((Me.ShowPreview AndAlso Me.previewWindow.PreviewMode) AndAlso (Not nowhere.AssociatedDesigner Is Me)) Then
                    nowhere = HitTestInfo.Nowhere
                End If
                Return nowhere
            End If
            Return MyBase.HitTest(point)
        End Function

        Protected Overrides Sub Initialize(ByVal activity As Activity)
            MyBase.Initialize(activity)
            Me.ShowPreview = True
            Dim activity2 As CompositeActivity = [TryCast](MyBase.Activity,CompositeActivity)
            If (Not activity2 Is Nothing) Then
                Dim activity3 As Activity
                For Each activity3 In activity2.Activities
                    If Not Helpers.IsAlternateFlowActivity(activity3) Then
                        Dim item As New ItemInfo(activity3.GetHashCode)
                        item.UserData.Item(DesignerUserDataKeys.Activity) = activity3
                        Me.previewStrip.Items.Add(item)
                    End If
                Next
            End If
            Dim service As ISelectionService = [TryCast](MyBase.GetService(GetType(ISelectionService)),ISelectionService)
            If (Not service Is Nothing) Then
                AddHandler service.SelectionChanged, New EventHandler(AddressOf Me.OnSelectionChanged)
            End If
        End Sub

        Public Overrides Function IsContainedDesignerVisible(ByVal containedDesigner As ActivityDesigner) As Boolean
            If (containedDesigner Is Nothing) Then
                Throw New ArgumentNullException("containedDesigner")
            End If
            If (Not MyBase.ActiveDesigner Is Me) Then
                Return MyBase.IsContainedDesignerVisible(containedDesigner)
            End If
            If (Me.ShowPreview AndAlso Me.previewWindow.PreviewMode) Then
                Return False
            End If
            Return ((Not Me.previewStrip.ActiveItem Is Nothing) AndAlso (Me.previewStrip.ActiveItem.UserData.Item(DesignerUserDataKeys.Activity) Is containedDesigner.Activity))
        End Function

        Protected Overrides Sub LoadViewState(ByVal reader As BinaryReader)
            If (reader Is Nothing) Then
                Throw New ArgumentNullException("reader")
            End If
            Dim num As Integer = reader.ReadInt32
            If ((num <> -1) AndAlso (num < Me.previewStrip.Items.Count)) Then
                Dim info As ItemInfo = Me.previewStrip.Items.Item(num)
                Dim service As IDesignerHost = [TryCast](MyBase.GetService(GetType(IDesignerHost)),IDesignerHost)
                If (service Is Nothing) Then
                    Throw New Exception(SR.GetString("General_MissingService", New Object() { GetType(IDesignerHost).FullName }))
                End If
                Me.EnsureVisibleContainedDesigner([TryCast](service.GetDesigner(DirectCast(info.UserData.Item(DesignerUserDataKeys.Activity), Activity)),ActivityDesigner))
            End If
            Dim flag As Boolean = reader.ReadBoolean
            If Me.ShowPreview Then
                Me.previewWindow.PreviewMode = flag
            End If
            MyBase.LoadViewState(reader)
        End Sub

        Private Sub OnChangePreviewMode(ByVal sender As Object, ByVal args As EventArgs)
            Dim verb As DesignerVerb = [TryCast](sender,DesignerVerb)
            If ((Me.ShowPreview AndAlso (Not verb Is Nothing)) AndAlso verb.Properties.Contains(DesignerUserDataKeys.PreviewActivity)) Then
                Me.previewWindow.PreviewMode = CBool(verb.Properties.Item(DesignerUserDataKeys.PreviewActivity))
            End If
        End Sub

        Protected Overrides Sub OnContainedActivitiesChanged(ByVal listChangeArgs As ActivityCollectionChangeEventArgs)
            If (MyBase.ActiveDesigner Is Me) Then
                If (((listChangeArgs.Action = ActivityCollectionChangeAction.Add) AndAlso (listChangeArgs.AddedItems.Count > 0)) AndAlso Not Helpers.IsAlternateFlowActivity(listChangeArgs.AddedItems.Item(0))) Then
                    Dim item As New ItemInfo(listChangeArgs.AddedItems.Item(0).GetHashCode)
                    item.UserData.Item(DesignerUserDataKeys.Activity) = listChangeArgs.AddedItems.Item(0)
                    If (listChangeArgs.Index < Me.previewStrip.Items.Count) Then
                        Me.previewStrip.Items.Insert(listChangeArgs.Index, item)
                    Else
                        Me.previewStrip.Items.Add(item)
                    End If
                ElseIf ((listChangeArgs.Action = ActivityCollectionChangeAction.Remove) AndAlso (listChangeArgs.RemovedItems.Count > 0)) Then
                    Dim index As Integer = Me.previewStrip.Items.IndexOf(New ItemInfo(listChangeArgs.RemovedItems.Item(0).GetHashCode))
                    If (index >= 0) Then
                        Me.previewStrip.Items.RemoveAt(index)
                    End If
                End If
            End If
            MyBase.OnContainedActivitiesChanged(listChangeArgs)
        End Sub

        Protected Overrides Sub OnDragEnter(ByVal e As ActivityDragEventArgs)
            MyBase.OnDragEnter(e)
            e.DragImageSnapPoint = Me.SnapInToPreviewStripDropTarget(e)
        End Sub

        Protected Overrides Sub OnDragOver(ByVal e As ActivityDragEventArgs)
            MyBase.OnDragOver(e)
            e.DragImageSnapPoint = Me.SnapInToPreviewStripDropTarget(e)
        End Sub

        Protected Overrides Sub OnLayoutPosition(ByVal e As ActivityDesignerLayoutEventArgs)
            If (e Is Nothing) Then
                Throw New ArgumentNullException("e")
            End If
            If ((MyBase.ActiveDesigner Is Me) AndAlso Me.Expanded) Then
                Dim bounds As Rectangle = MyBase.Bounds
                Me.previewStrip.Location = New Point(((bounds.Left + (bounds.Width / 2)) - (Me.previewStrip.Size.Width / 2)), ((Me.Location.Y + Me.TitleHeight) + e.AmbientTheme.Margin.Height))
                MyBase.OnLayoutPosition(e)
                If Me.ShowPreview Then
                    Dim rectangle2 As Rectangle = Me.previewStrip.Bounds
                    Me.previewWindow.Location = New Point(((bounds.Left + (bounds.Width / 2)) - (Me.previewWindow.Size.Width / 2)), (rectangle2.Bottom + (3 * e.AmbientTheme.Margin.Height)))
                    Me.separatorLine(0).X = (bounds.Left + e.AmbientTheme.Margin.Width)
                    Me.separatorLine(0).Y = rectangle2.Bottom
                    Me.separatorLine(0).Y = (Me.separatorLine(0).Y + (e.AmbientTheme.Margin.Height + (e.AmbientTheme.Margin.Height / 2)))
                    Me.separatorLine(1).X = (bounds.Right - e.AmbientTheme.Margin.Width)
                    Me.separatorLine(1).Y = rectangle2.Bottom
                    Me.separatorLine(1).Y = (Me.separatorLine(1).Y + (e.AmbientTheme.Margin.Height + (e.AmbientTheme.Margin.Height / 2)))
                Else
                    Dim num As Integer = (Me.previewStrip.Bounds.Bottom - Me.Location.Y)
                    If (Not Me.PreviewedDesigner Is Nothing) Then
                        Me.PreviewedDesigner.Location = New Point((Me.Location.X + ((Me.Size.Width - Me.PreviewedDesigner.Size.Width) / 2)), ((Me.Location.Y + num) + (2 * e.AmbientTheme.Margin.Height)))
                    End If
                End If
            Else
                MyBase.OnLayoutPosition(e)
            End If
        End Sub

        Protected Overrides Function OnLayoutSize(ByVal e As ActivityDesignerLayoutEventArgs) As Size
            Dim size As Size = MyBase.OnLayoutSize(e)
            If (Not Me.Expanded OrElse (Not MyBase.ActiveDesigner Is Me)) Then
                Return size
            End If
            Me.previewStrip.OnLayoutSize(e.Graphics)
            Dim empty As Size = Size.Empty
            empty.Width = Math.Max(empty.Width, Me.previewStrip.Size.Width)
            empty.Height = (empty.Height + Me.previewStrip.Size.Height)
            empty.Height = (empty.Height + e.AmbientTheme.Margin.Height)
            If (Not Me.previewWindow Is Nothing) Then
                Me.previewWindow.Refresh
                Me.previewWindow.OnLayoutSize(e.Graphics, empty.Width)
                empty.Width = Math.Max(empty.Width, Me.previewWindow.Size.Width)
                empty.Width = (empty.Width + (2 * e.AmbientTheme.Margin.Width))
                empty.Height = (empty.Height + Me.TitleHeight)
                empty.Height = (empty.Height + (4 * e.AmbientTheme.Margin.Height))
                empty.Height = (empty.Height + Me.previewWindow.Size.Height)
                empty.Height = (empty.Height + e.AmbientTheme.Margin.Height)
            Else
                empty.Width = Math.Max(empty.Width, size.Width)
                empty.Width = (empty.Width + (3 * e.AmbientTheme.Margin.Width))
                empty.Width = (empty.Width + (2 * e.AmbientTheme.SelectionSize.Width))
                empty.Height = (empty.Height + size.Height)
            End If
            empty.Width = Math.Max(empty.Width, Me.MinimumSize.Width)
            empty.Height = Math.Max(empty.Height, Me.MinimumSize.Height)
            If (Not Me.ShowPreview AndAlso (Not Me.PreviewedDesigner Is Nothing)) Then
                Dim designerTheme As ActivityPreviewDesignerTheme = [TryCast](e.DesignerTheme,ActivityPreviewDesignerTheme)
                If (Not designerTheme Is Nothing) Then
                    empty.Height = (empty.Height - designerTheme.ConnectorSize.Height)
                    empty.Height = (empty.Height - (2 * e.AmbientTheme.Margin.Height))
                    empty.Height = (empty.Height - (2 * e.AmbientTheme.SelectionSize.Height))
                End If
                Dim size3 As New Size(((2 * e.AmbientTheme.Margin.Width) + (2 * e.AmbientTheme.SelectionSize.Width)), ((2 * e.AmbientTheme.Margin.Height) + (2 * e.AmbientTheme.SelectionSize.Height)))
                Me.PreviewedDesigner.Size = New Size((empty.Width - size3.Width), (empty.Height - ((Me.TitleHeight + Me.previewStrip.Size.Height) + size3.Height)))
            End If
            Return empty
        End Function

        Protected Overrides Sub OnMouseDoubleClick(ByVal e As MouseEventArgs)
            MyBase.OnMouseDoubleClick(e)
            If (MyBase.ActiveDesigner Is Me) Then
                Dim pt As New Point(e.X, e.Y)
                If (((Not Me.PreviewedDesigner Is Nothing) AndAlso Me.ShowPreview) AndAlso (Me.previewWindow.PreviewMode AndAlso Me.previewWindow.Bounds.Contains(pt))) Then
                    Me.previewWindow.PreviewMode = False
                End If
            End If
        End Sub

        Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
            MyBase.OnMouseDown(e)
            If (MyBase.ActiveDesigner Is Me) Then
                Dim pt As New Point(e.X, e.Y)
                If Me.previewStrip.Bounds.Contains(pt) Then
                    Me.previewStrip.OnMouseDown(e)
                ElseIf (Me.ShowPreview AndAlso Me.previewWindow.Bounds.Contains(pt)) Then
                    Me.previewWindow.OnMouseDown(e)
                End If
            End If
        End Sub

        Protected Overrides Sub OnMouseLeave()
            MyBase.OnMouseLeave
            If (MyBase.ActiveDesigner Is Me) Then
                Me.previewStrip.OnMouseLeave
            End If
        End Sub

        Protected Overrides Sub OnMouseUp(ByVal e As MouseEventArgs)
            MyBase.OnMouseUp(e)
            If (MyBase.ActiveDesigner Is Me) Then
                Me.previewStrip.OnMouseUp(e)
            End If
        End Sub

        Protected Overrides Sub OnPaint(ByVal e As ActivityDesignerPaintEventArgs)
            If ((Me.ShowPreview AndAlso Not Me.previewWindow.PreviewMode) AndAlso (Not Me.PreviewedDesigner Is Nothing)) Then
                Me.removePreviewedDesigner = True
            End If
            MyBase.OnPaint(e)
            If ((Me.ShowPreview AndAlso Not Me.previewWindow.PreviewMode) AndAlso (Not Me.PreviewedDesigner Is Nothing)) Then
                Me.removePreviewedDesigner = False
            End If
            If (Me.Expanded AndAlso (MyBase.ActiveDesigner Is Me)) Then
                Me.previewStrip.Draw(e.Graphics)
                If Me.ShowPreview Then
                    e.Graphics.DrawLine(e.DesignerTheme.ForegroundPen, Me.separatorLine(0), Me.separatorLine(1))
                    Me.previewWindow.Draw(e.Graphics, e.ViewPort)
                End If
            End If
        End Sub

        Private Sub OnPreviewChanged(ByVal sender As Object, ByVal e As SelectionChangeEventArgs)
            Dim flag As Boolean = Not Me.ShowPreview
            If Me.ShowPreview Then
                Me.previewWindow.PreviewedActivity = If((Not e.CurrentItem Is Nothing), [TryCast](e.CurrentItem.UserData.Item(DesignerUserDataKeys.Activity),Activity), Nothing)
                flag = Not Me.previewWindow.PreviewMode
            End If
            If flag Then
                MyBase.PerformLayout
            End If
        End Sub

        Private Sub OnPreviewModeChanged(ByVal sender As Object, ByVal e As EventArgs)
            MyBase.RefreshDesignerVerbs
        End Sub

        Private Sub OnPreviewModeStatusUpdate(ByVal sender As Object, ByVal args As EventArgs)
            Dim verb As DesignerVerb = [TryCast](sender,DesignerVerb)
            If ((Me.ShowPreview AndAlso (Not verb Is Nothing)) AndAlso verb.Properties.Contains(DesignerUserDataKeys.PreviewActivity)) Then
                verb.Enabled = (Me.previewWindow.PreviewMode <> CBool(verb.Properties.Item(DesignerUserDataKeys.PreviewActivity)))
            End If
        End Sub

        Private Sub OnSelectionChanged(ByVal sender As Object, ByVal e As EventArgs)
            Dim service As ISelectionService = [TryCast](MyBase.GetService(GetType(ISelectionService)),ISelectionService)
            If ((Not service Is Nothing) AndAlso (MyBase.ActiveDesigner Is Me)) Then
                Dim info As ItemInfo
                For Each info In Me.previewStrip.Items
                    If (info.UserData.Item(DesignerUserDataKeys.Activity) Is service.PrimarySelection) Then
                        Me.previewStrip.ActiveItem = info
                        Exit For
                    End If
                Next
                If (service.SelectionCount = 1) Then
                    Dim designer As ActivityDesigner = ActivityDesigner.GetDesigner([TryCast](service.PrimarySelection,Activity))
                    If (((Not designer Is Nothing) AndAlso Not designer.IsVisible) AndAlso (Not Me Is designer.ParentDesigner)) Then
                        Dim designer2 As ActivityDesigner = designer
                        Do While (Not designer2 Is Nothing)
                            Dim parentDesigner As CompositeActivityDesigner = designer2.ParentDesigner
                            If (Me Is parentDesigner) Then
                                Exit Do
                            End If
                            designer2 = parentDesigner
                        Loop
                        If (Not designer2 Is Nothing) Then
                            If ((Not Me.previewWindow Is Nothing) AndAlso Me.previewWindow.PreviewMode) Then
                                designer2.EnsureVisible
                            Else
                                designer.EnsureVisible
                            End If
                        End If
                    End If
                End If
            End If
        End Sub

        Protected Overrides Sub OnThemeChange(ByVal newTheme As ActivityDesignerTheme)
            MyBase.OnThemeChange(newTheme)
            Me.RefreshPreview
        End Sub

        Private Sub OnViewActivity(ByVal sender As Object, ByVal args As EventArgs)
            Dim verb As DesignerVerb = [TryCast](sender,DesignerVerb)
            If ((Not verb Is Nothing) AndAlso verb.Properties.Contains(DesignerUserDataKeys.ViewActivity)) Then
                Dim activeItem As ItemInfo = Me.previewStrip.ActiveItem
                If (Not activeItem Is Nothing) Then
                    Dim flag As Boolean = CBool(verb.Properties.Item(DesignerUserDataKeys.ViewActivity))
                    Dim num As Integer = (Me.previewStrip.Items.IndexOf(activeItem) + If(flag, 1, -1))
                    num = If((num >= Me.previewStrip.Items.Count), 0, If((num < 0), (Me.previewStrip.Items.Count - 1), num))
                    Me.previewStrip.ActiveItem = Me.previewStrip.Items.Item(num)
                End If
            End If
        End Sub

        Private Sub OnViewActivityStatusUpdate(ByVal sender As Object, ByVal args As EventArgs)
            Dim verb As DesignerVerb = [TryCast](sender,DesignerVerb)
            If (Not verb Is Nothing) Then
                verb.Enabled = (((Not Me.previewStrip.ActiveItem Is Nothing) AndAlso (Me.previewStrip.Items.Count > 1)) AndAlso (Me.previewStrip.Items.IndexOf(Me.previewStrip.ActiveItem) >= 0))
            End If
        End Sub

        Protected Friend Overrides Sub RefreshDesignerActions()
            MyBase.RefreshDesignerActions
            Me.RefreshPreview
        End Sub

        Public Sub RefreshPreview()
            If Me.ShowPreview Then
                Me.previewWindow.Refresh
            End If
        End Sub

        Protected Overrides Sub SaveViewState(ByVal writer As BinaryWriter)
            If (writer Is Nothing) Then
                Throw New ArgumentNullException("writer")
            End If
            Dim index As Integer = -1
            Dim previewMode As Boolean = False
            If (Not Me.previewStrip.ActiveItem Is Nothing) Then
                index = Me.previewStrip.Items.IndexOf(Me.previewStrip.ActiveItem)
            End If
            writer.Write(index)
            If Me.ShowPreview Then
                previewMode = Me.previewWindow.PreviewMode
            End If
            writer.Write(previewMode)
            MyBase.SaveViewState(writer)
        End Sub

        Private Function SnapInToPreviewStripDropTarget(ByVal e As ActivityDragEventArgs) As Point
            Dim activeDropTarget As Integer = Me.previewStrip.ActiveDropTarget
            Dim dropTargets As Rectangle() = Me.previewStrip.DropTargets
            If ((activeDropTarget < 0) OrElse (activeDropTarget >= dropTargets.Length)) Then
                Return Point.Empty
            End If
            Dim rectangle As Rectangle = dropTargets(activeDropTarget)
            Dim designerTheme As ActivityPreviewDesignerTheme = [TryCast](MyBase.DesignerTheme,ActivityPreviewDesignerTheme)
            rectangle.Width = If((rectangle.Width > If((Not designerTheme Is Nothing), designerTheme.ConnectorSize.Width, 0)), If((Not designerTheme Is Nothing), designerTheme.ConnectorSize.Width, 0), rectangle.Width)
            Return New Point((rectangle.Left + (rectangle.Width / 2)), (rectangle.Top + (rectangle.Height / 2)))
        End Function

        Private Function StripItemIndexFromActivity(ByVal activity As Activity) As Integer
            Dim num As Integer = 0
            Dim info As ItemInfo
            For Each info In Me.previewStrip.Items
                If (info.UserData.Item(DesignerUserDataKeys.Activity) Is activity) Then
                    Return num
                End If
                num += 1
            Next
            Return -1
        End Function


        ' Properties
        Public Overrides ReadOnly Property AccessibilityObject As AccessibleObject
            Get
                If (Me.accessibilityObject Is Nothing) Then
                    Me.accessibilityObject = New ActivityCollectionAccessibleObject(Me)
                End If
                Return Me.accessibilityObject
            End Get
        End Property

        Public Overrides ReadOnly Property ContainedDesigners As ReadOnlyCollection([Of] ActivityDesigner)
            Get
                If (Not MyBase.ActiveDesigner Is Me) Then
                    Return MyBase.ContainedDesigners
                End If
                Dim list As New List([Of] ActivityDesigner)
                If (Not Me.PreviewedDesigner Is Nothing) Then
                    If Me.ShowPreview Then
                        list.AddRange(MyBase.ContainedDesigners)
                        If Me.removePreviewedDesigner Then
                            list.Remove(Me.PreviewedDesigner)
                        End If
                    Else
                        list.Add(Me.PreviewedDesigner)
                    End If
                End If
                Return list.AsReadOnly
            End Get
        End Property

        Protected Overrides Property CurrentDropTarget As Integer
            Get
                Return MyBase.CurrentDropTarget
            End Get
            Set(ByVal value As Integer)
                MyBase.CurrentDropTarget = value
                Me.previewStrip.ActiveDropTarget = value
            End Set
        End Property

        Public Overrides ReadOnly Property FirstSelectableObject As Object
            Get
                If (Me.Expanded AndAlso Me.IsVisible) Then
                    If (Not Me.PreviewedDesigner Is Nothing) Then
                        Return Me.PreviewedDesigner.Activity
                    End If
                    If (Me.ContainedDesigners.Count > 0) Then
                        Return Me.ContainedDesigners.Item(0).Activity
                    End If
                End If
                Return Nothing
            End Get
        End Property

        Protected Friend Overrides ReadOnly Property Glyphs As ActivityDesignerGlyphCollection
            Get
                Dim glyphs As New ActivityDesignerGlyphCollection
                If (Me.Expanded AndAlso (MyBase.ActiveDesigner Is Me)) Then
                    Dim service As ISelectionService = [TryCast](MyBase.GetService(GetType(ISelectionService)),ISelectionService)
                    Dim loader As WorkflowDesignerLoader = [TryCast](MyBase.GetService(GetType(WorkflowDesignerLoader)),WorkflowDesignerLoader)
                    Dim flag As Boolean = ((Not loader Is Nothing) AndAlso Not loader.InDebugMode)
                    Dim info As ItemInfo
                    For Each info In Me.previewStrip.Items
                        Dim itemBounds As Rectangle = Me.previewStrip.GetItemBounds(info)
                        Dim component As Activity = [TryCast](info.UserData.Item(DesignerUserDataKeys.Activity),Activity)
                        If ((Not component Is Nothing) AndAlso Not itemBounds.IsEmpty) Then
                            If ((Not service Is Nothing) AndAlso service.GetComponentSelected(component)) Then
                                glyphs.Add(New StripItemSelectionGlyph(Me, info))
                            End If
                            If (Not component.Enabled AndAlso Not ActivityDesigner.IsCommentedActivity(component)) Then
                                glyphs.Add(New StripItemCommentGlyph(Me, info))
                            End If
                            If ((Me.ShowPreview AndAlso flag) AndAlso Me.Expanded) Then
                                Dim designer As ActivityDesigner = ActivityDesigner.GetDesigner(component)
                                If (((Not designer Is Nothing) AndAlso (designer.DesignerActions.Count > 0)) AndAlso flag) Then
                                    glyphs.Add(New StripItemConfigErrorGlyph(Me, info))
                                End If
                            End If
                        End If
                    Next
                End If
                glyphs.AddRange(MyBase.Glyphs)
                Return glyphs
            End Get
        End Property

        Protected Overrides Property HelpText As String
            Get
                Return MyBase.HelpText
            End Get
            Set(ByVal value As String)
                MyBase.HelpText = value
                Me.previewStrip.HelpText = value
            End Set
        End Property

        Public Overrides ReadOnly Property LastSelectableObject As Object
            Get
                If (Me.Expanded AndAlso Me.IsVisible) Then
                    If (Not Me.PreviewedDesigner Is Nothing) Then
                        Dim previewedDesigner As CompositeActivityDesigner = [TryCast](Me.PreviewedDesigner,CompositeActivityDesigner)
                        If (((Not Me.previewWindow Is Nothing) AndAlso Not Me.previewWindow.PreviewMode) AndAlso (Not previewedDesigner Is Nothing)) Then
                            Return previewedDesigner.LastSelectableObject
                        End If
                        Return Me.PreviewedDesigner.Activity
                    End If
                    If (Me.ContainedDesigners.Count > 0) Then
                        Return Me.ContainedDesigners.Item((Me.ContainedDesigners.Count - 1)).Activity
                    End If
                End If
                Return Nothing
            End Get
        End Property

        Public Overrides Property Location As Point
            Get
                Return MyBase.Location
            End Get
            Set(ByVal value As Point)
                If (MyBase.Location <> value) Then
                    Dim size As New Size((value.X - MyBase.Location.X), (value.Y - MyBase.Location.Y))
                    MyBase.Location = value
                    Me.previewStrip.Location = New Point((Me.previewStrip.Location.X + size.Width), (Me.previewStrip.Location.Y + size.Height))
                    If Me.ShowPreview Then
                        Me.previewWindow.Location = New Point((Me.previewWindow.Location.X + size.Width), (Me.previewWindow.Location.Y + size.Height))
                        Me.separatorLine(0) = New Point((Me.separatorLine(0).X + size.Width), (Me.separatorLine(0).Y + size.Height))
                        Me.separatorLine(1) = New Point((Me.separatorLine(1).X + size.Width), (Me.separatorLine(1).Y + size.Height))
                    Else
                        Dim num As Integer = (Me.previewStrip.Bounds.Bottom - Me.Location.Y)
                        If (Not Me.PreviewedDesigner Is Nothing) Then
                            Me.PreviewedDesigner.Location = New Point((Me.Location.X + ((Me.Size.Width - Me.PreviewedDesigner.Size.Width) / 2)), ((Me.Location.Y + num) + (2 * WorkflowTheme.CurrentTheme.AmbientTheme.Margin.Height)))
                        End If
                    End If
                End If
            End Set
        End Property

        Public ReadOnly Property PreviewedDesigner As ActivityDesigner
            Get
                Dim activeItem As ItemInfo = Me.previewStrip.ActiveItem
                If (activeItem Is Nothing) Then
                    Return Nothing
                End If
                Return ActivityDesigner.GetDesigner([TryCast](activeItem.UserData.Item(DesignerUserDataKeys.Activity),Activity))
            End Get
        End Property

        Public Property ShowPreview As Boolean
            Get
                Return (Not Me.previewWindow Is Nothing)
            End Get
            Set(ByVal value As Boolean)
                If (Me.ShowPreview <> value) Then
                    If (Not Me.previewWindow Is Nothing) Then
                        RemoveHandler Me.previewWindow.PreviewModeChanged, New EventHandler(AddressOf Me.OnPreviewModeChanged)
                        Me.previewWindow = Nothing
                    Else
                        Me.previewWindow = New PreviewWindow(Me)
                        AddHandler Me.previewWindow.PreviewModeChanged, New EventHandler(AddressOf Me.OnPreviewModeChanged)
                    End If
                    Me.designerVerbs = Nothing
                    TypeDescriptor.Refresh(MyBase.Activity)
                    MyBase.RefreshDesignerVerbs
                End If
            End Set
        End Property

        Protected Overrides ReadOnly Property Verbs As ActivityDesignerVerbCollection
            Get
                Dim verbs As New ActivityDesignerVerbCollection
                verbs.AddRange(MyBase.Verbs)
                If (Me.designerVerbs Is Nothing) Then
                    Me.designerVerbs = New ActivityDesignerVerbCollection
                    If (MyBase.ActiveDesigner Is Me) Then
                        Dim verb As DesignerVerb = New ActivityDesignerVerb(Me, DesignerVerbGroup.View, DR.GetString("ViewPreviousActivity", New Object(0  - 1) {}), New EventHandler(AddressOf Me.OnViewActivity), New EventHandler(AddressOf Me.OnViewActivityStatusUpdate))
                        verb.Properties.Item(DesignerUserDataKeys.ViewActivity) = False
                        Me.designerVerbs.Add(verb)
                        verb = New ActivityDesignerVerb(Me, DesignerVerbGroup.View, DR.GetString("ViewNextActivity", New Object(0  - 1) {}), New EventHandler(AddressOf Me.OnViewActivity), New EventHandler(AddressOf Me.OnViewActivityStatusUpdate))
                        verb.Properties.Item(DesignerUserDataKeys.ViewActivity) = True
                        Me.designerVerbs.Add(verb)
                        If Me.ShowPreview Then
                            verb = New ActivityDesignerVerb(Me, DesignerVerbGroup.Edit, DR.GetString("PreviewActivity", New Object(0  - 1) {}), New EventHandler(AddressOf Me.OnChangePreviewMode), New EventHandler(AddressOf Me.OnPreviewModeStatusUpdate))
                            verb.Properties.Item(DesignerUserDataKeys.PreviewActivity) = True
                            Me.designerVerbs.Add(verb)
                            verb = New ActivityDesignerVerb(Me, DesignerVerbGroup.Edit, DR.GetString("EditActivity", New Object(0  - 1) {}), New EventHandler(AddressOf Me.OnChangePreviewMode), New EventHandler(AddressOf Me.OnPreviewModeStatusUpdate))
                            verb.Properties.Item(DesignerUserDataKeys.PreviewActivity) = False
                            Me.designerVerbs.Add(verb)
                        End If
                    End If
                End If
                verbs.AddRange(Me.designerVerbs)
                Return verbs
            End Get
        End Property


        ' Fields
        Private accessibilityObject As ActivityCollectionAccessibleObject
        Private designerVerbs As ActivityDesignerVerbCollection
        Private previewStrip As PreviewItemStrip
        Private previewWindow As PreviewWindow
        Private removePreviewedDesigner As Boolean
        Private separatorLine As Point() = New Point(2  - 1) {}

        ' Nested Types
        Private NotInheritable Class ActivityCollectionAccessibleObject
            Inherits SequenceDesignerAccessibleObject
            ' Methods
            Public Sub New(ByVal activityDesigner As ActivityPreviewDesigner)
                MyBase.New(activityDesigner)
            End Sub

            Public Overrides Function GetChild(ByVal index As Integer) As AccessibleObject
                Dim activityDesigner As ActivityPreviewDesigner = [TryCast](MyBase.ActivityDesigner,ActivityPreviewDesigner)
                If ((activityDesigner Is Nothing) OrElse (Not activityDesigner.ActiveDesigner Is activityDesigner)) Then
                    Return MyBase.GetChild(index)
                End If
                If (index < activityDesigner.previewStrip.AccessibilityObjects.Length) Then
                    Return activityDesigner.previewStrip.AccessibilityObjects(index)
                End If
                index = (index - activityDesigner.previewStrip.AccessibilityObjects.Length)
                If (activityDesigner.ShowPreview AndAlso (index = 0)) Then
                    Return activityDesigner.previewWindow.AccessibilityObject
                End If
                Dim accessibilityObject As AccessibleObject = activityDesigner.PreviewedDesigner.AccessibilityObject
                Do While (accessibilityObject.Bounds.Size.IsEmpty AndAlso (accessibilityObject.GetChildCount > 0))
                    accessibilityObject = accessibilityObject.GetChild(0)
                Loop
                Return accessibilityObject
            End Function

            Public Overrides Function GetChildCount() As Integer
                Dim num As Integer = 0
                Dim activityDesigner As ActivityPreviewDesigner = [TryCast](MyBase.ActivityDesigner,ActivityPreviewDesigner)
                If ((Not activityDesigner Is Nothing) AndAlso (activityDesigner.ActiveDesigner Is activityDesigner)) Then
                    num = (num + activityDesigner.previewStrip.AccessibilityObjects.Length)
                    If activityDesigner.ShowPreview Then
                        num += 1
                    End If
                    If ((Not activityDesigner.ShowPreview OrElse activityDesigner.previewWindow.PreviewMode) AndAlso activityDesigner.ShowPreview) Then
                        Return num
                    End If
                    num += 1
                End If
                Return num
            End Function

        End Class

        Private NotInheritable Class StripItemCommentGlyph
            Inherits CommentGlyph
            ' Methods
            Friend Sub New(ByVal parentDesigner As ActivityPreviewDesigner, ByVal item As ItemInfo)
                Me.parentDesigner = parentDesigner
                Me.item = item
            End Sub

            Public Overrides Function GetBounds(ByVal designer As ActivityDesigner, ByVal activated As Boolean) As Rectangle
                Return Me.parentDesigner.previewStrip.GetItemBounds(Me.item)
            End Function


            ' Fields
            Private item As ItemInfo
            Private parentDesigner As ActivityPreviewDesigner
        End Class

        Private NotInheritable Class StripItemConfigErrorGlyph
            Inherits ConfigErrorGlyph
            ' Methods
            Friend Sub New(ByVal parentDesigner As ActivityPreviewDesigner, ByVal item As ItemInfo)
                Me.parentDesigner = parentDesigner
                Me.item = item
            End Sub

            Public Overrides Function GetBounds(ByVal designer As ActivityDesigner, ByVal activated As Boolean) As Rectangle
                Dim itemBounds As Rectangle = Me.parentDesigner.previewStrip.GetItemBounds(Me.item)
                Dim glyphSize As Size = WorkflowTheme.CurrentTheme.AmbientTheme.GlyphSize
                glyphSize.Width = ((glyphSize.Width * 3) / 4)
                glyphSize.Height = ((glyphSize.Height * 3) / 4)
                Dim location As New Point((itemBounds.Right - (glyphSize.Width / 2)), (itemBounds.Top - (glyphSize.Height / 2)))
                itemBounds = New Rectangle(location, glyphSize)
                If activated Then
                    itemBounds.Width = (itemBounds.Width * 2)
                    Dim ambientTheme As AmbientTheme = WorkflowTheme.CurrentTheme.AmbientTheme
                    itemBounds.Inflate((ambientTheme.Margin.Width / 2), (ambientTheme.Margin.Height / 2))
                End If
                Return itemBounds
            End Function

            Protected Overrides Sub OnActivate(ByVal designer As ActivityDesigner)
                Dim designer2 As ActivityDesigner = ActivityDesigner.GetDesigner([TryCast](Me.item.UserData.Item(DesignerUserDataKeys.Activity),Activity))
                If (Not designer2 Is Nothing) Then
                    MyBase.OnActivate(designer2)
                End If
            End Sub


            ' Fields
            Private item As ItemInfo
            Private parentDesigner As ActivityPreviewDesigner
        End Class

        Private NotInheritable Class StripItemSelectionGlyph
            Inherits SelectionGlyph
            ' Methods
            Friend Sub New(ByVal parentDesigner As ActivityPreviewDesigner, ByVal item As ItemInfo)
                Me.parentDesigner = parentDesigner
                Me.item = item
            End Sub

            Public Overrides Function GetBounds(ByVal designer As ActivityDesigner, ByVal activated As Boolean) As Rectangle
                Dim itemBounds As Rectangle = Me.parentDesigner.previewStrip.GetItemBounds(Me.item)
                Dim size As New Size(Math.Max((itemBounds.Width / 6), 1), Math.Max((itemBounds.Height / 6), 1))
                itemBounds.Inflate(size)
                Return itemBounds
            End Function


            ' Properties
            Public Overrides ReadOnly Property IsPrimarySelection As Boolean
                Get
                    Dim service As ISelectionService = [TryCast](Me.parentDesigner.GetService(GetType(ISelectionService)),ISelectionService)
                    Return ((Not service Is Nothing) AndAlso (service.PrimarySelection Is Me.item.UserData.Item(DesignerUserDataKeys.Activity)))
                End Get
            End Property


            ' Fields
            Private item As ItemInfo
            Private parentDesigner As ActivityPreviewDesigner
        End Class
    End Class
End Namespace

